<template>
  <div class="app-container" v-loading="dbLoading">
    <h2 style="text-align: center">字体和七步法</h2>

    <el-steps
      :active="4"
      process-status="wait"
      align-center
      style="margin-bottom: 40px"
    >
      <el-step title="添加字体" />
      <el-step title="添加七步" />
    </el-steps>
    <div>
      <el-form :inline="true">
        <el-form-item>
          <el-select
            v-model="buzhou"
            placeholder="请选择类型"
            @change="buzhouChange"
          >
            <el-option label="欣赏" :value="0"> </el-option>
            <el-option label="认读" :value="1"> </el-option>
            <el-option label="观察" :value="2"> </el-option>
            <el-option label="分析" :value="4"> </el-option>
            <el-option label="笔法" :value="3"> </el-option>
            <el-option label="视频" :value="7"> </el-option>
            <el-option label="对比" :value="5"> </el-option>
            <el-option label="随堂练习" :value="6"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addContrast(0)" :disabled="xzBtn"
            >新增</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="dbList" stripe v-loading="dbLoading" style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-table :data="props.row.children" stripe style="width: 100%">
            <el-table-column
              prop="contrastType"
              header-align="center"
              align="center"
              label="对比类型"
            >
            </el-table-column>
            <el-table-column
              prop="contrastSonpic"
              header-align="center"
              align="center"
              label="对比子图"
            >
              <template slot-scope="scope">
                <el-image :src="scope.row.contrastSonpic"></el-image></template
            ></el-table-column>
            <el-table-column
              prop="corder"
              header-align="center"
              align="center"
              label="顺序"
            ></el-table-column>
            <el-table-column
              header-align="center"
              align="center"
              label="对比字体视频"
              prop="contrastVideo"
            >
              <template slot-scope="scope">
                <video
                  :src="scope.row.contrastVideo"
                  controls="controls"
                  class="avatar"
                >
                  您的浏览器不支持视频播放
                </video>
              </template>
            </el-table-column>
            <el-table-column header-align="center" align="center" label="操作">
              <template slot-scope="scope">
                <el-button
                  type="text"
                  @click="addContrastTypeface(1, scope.row.id)"
                  >修改</el-button
                >
                <el-button type="text" @click="deleteById2(scope.row.id)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="对比id"
      ></el-table-column>
      <el-table-column
        prop="contrastDescription"
        header-align="center"
        align="center"
        label="对比描述"
      >
      </el-table-column>
      <el-table-column header-align="center" align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="addContrastTypeface(0, scope.row.id)"
            >添加图片</el-button
          >
          <el-button style="" type="text" @click="addContrast(1, scope.row.id)"
            >修改</el-button
          >
          <!-- <el-button type="text" @click="deleteById(scope.row.id)"
              >删除</el-button
            > -->
        </template>
      </el-table-column>
    </el-table>
    <!-- <ul class="chanpterList">
      <li v-if="contrast.id != null" :model="contrast" :key="contrast.id">
        <p>
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4 }"
            v-model="contrast.contrastDescription"
          >
          </el-input>

          <span class="acts">
            <el-button type="text" @click="addContrastTypeface(0, contrast.id)"
              >添加图片</el-button
            >
            <el-button style="" type="text" @click="addContrast(1, contrast.id)"
              >修改</el-button
            >
          </span>
        </p>

        <ul class="chanpterList videoList">
          <li
            v-for="contrastTypeface in contrast.children"
            :key="contrastTypeface.id"
          >
            <p>
              {{ contrastTypeface.contrastType }}
              <span class="acts">
                <el-button
                  type="text"
                  @click="addContrastTypeface(1, contrastTypeface.id)"
                  >修改</el-button
                >
                <el-button type="text" @click="deleteById2(contrastTypeface.id)"
                  >删除</el-button
                >
              </span>
            </p>
          </li>
        </ul>
      </li>
    </ul> -->
    <el-dialog
      :visible.sync="dialogContrastVisible"
      :title="type == 0 ? '添加对比信息' : '修改对比信息'"
    >
      <el-form
        :rules="dataRule"
        ref="contrastForm"
        :model="contrast"
        label-width="120px"
      >
        <el-form-item label="对比描述" prop="contrastDescription">
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4 }"
            placeholder="请输入对比描述"
            v-model="contrast.contrastDescription"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogContrastVisible = false">取 消</el-button>
        <el-button
          :disabled="saveContrastBtnDisabled"
          type="primary"
          @click="saveOrUpdate"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <el-dialog
      :visible.sync="dialogContrastTypefaceVisible"
      :title="type == 0 ? '添加对比字体信息' : '修改对比字体信息'"
    >
      <el-form
        :rules="dataRule1"
        ref="contrastTypefaceForm"
        :model="contrastTypeface"
        label-width="120px"
      >
        <el-form-item label="对比视频" prop="contrastVideo">
          <el-upload
            class="upload-uploader"
            :action="'http://121.41.167.195:8000/api/oss/upload'"
            ref="videoUpload"
            :on-exceed="handleExceed"
            :on-remove="handleRemove"
            :on-success="handleAudioSuccess"
            :before-upload="beforeVideoUpload"
            :show-file-list="false"
          >
            <video
              :src="contrastTypeface.contrastVideo"
              controls="controls"
              class="avatar"
            >
              您的浏览器不支持视频播放
            </video>
          </el-upload>
        </el-form-item>
        <el-form-item label="对比子图" prop="contrastSonpic">
          <el-upload
            class="avatar-uploader"
            :action="'http://121.41.167.195:8000/api/oss/upload'"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img
              v-if="contrastTypeface.contrastSonpic"
              :src="contrastTypeface.contrastSonpic"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="对比类型" prop="contrastType">
          <!-- <el-input v-model="contrastTypeface.contrastType" /> -->
          <el-select
            v-model="contrastTypeface.contrastType"
            placeholder="请选择类型"
          >
            <el-option label="对比颜勤" value="对比颜勤"> </el-option>
            <el-option label="对比颜多" value="对比颜多"> </el-option>
            <el-option label="对比赵体" value="对比赵体"> </el-option>
            <el-option label="对比欧体" value="对比欧体"> </el-option>
            <el-option label="对比柳体" value="对比柳体"> </el-option>
            <el-option label="对比现代" value="对比现代"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="顺序" prop="corder">
          <el-input-number
            v-model="contrastTypeface.corder"
            controls-position="right"
            :min="1"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogContrastTypefaceVisible = false"
          >取 消</el-button
        >
        <el-button
          :disabled="saveContrastTypefaceBtnDisabled"
          type="primary"
          @click="saveOrUpdate1"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <div>
      <el-button @click="previous">上一步</el-button>
      <!-- <el-button :disabled="saveBtnDisabled" type="primary" @click="next"
        >完成</el-button
      > -->
    </div>
  </div>
</template>

<script>
import Vue from "vue";
export default {
  data() {
    return {
      dbList: [],
      buzhou: 5,
      dbLoading: false,
      saveBtnDisabled: false, // 保存按钮是否禁用
      contrast: {
        // 出版社对象
        contrastDescription: "一、观察该字，并与源各碑帖相同的主要笔画，在大小、长短、方圆、曲直、以及取势的变化。\r\n二、对比该字与其它碑帖上的字笔画、结构、字形特点在米字格中布局安排的位置有什么不同？\r\n三、对比后总结出在临摹书写该字时应该注意哪些?",
        tid: null,
        id: null,
      },
      contrastTypeface: {
        id: null,
        contrastVideo: "",
        contrastSonpic: "",
        contrastType: "",
        corder: 1,
        cid: null,
      },
      xzBtn: false,
      type: 0, // 0:添加1:修改
      dialogContrastVisible: false,
      saveContrastBtnDisabled: false,
      dialogContrastTypefaceVisible: false,
      saveContrastTypefaceBtnDisabled: false,
      dataRule: {
        contrastDescription: [{ required: true, message: "对比描述不能为空" }],
      },
      dataRule1: {
        corder: [{ required: true, message: "顺序不能为空", trigger: "blur" }],
        contrastType: [
          { required: true, message: "对比类型不能为空", trigger: "blur" },
        ],
        contrastSonpic: [{ required: true, message: "图片不能为空" }],
        contrastVideo: [{ required: true, message: "视频不能为空" }],
      },
    };
  },

  activated() {
    this.buzhou = 5;
    this.contrast.tid = this.$route.params.id;
    // 有数据就要回显数据
    this.getContrastByWordId();
  },

  methods: {
    buzhouChange(value) {
      switch (value) {
        case 0:
          // 欣赏
          this.$router.push({
            path: "/ruanbi/ruanbixsAdd/" + this.$route.params.id,
          });
          break;
        case 1:
          // 认读
          this.$router.push({
            path: "/ruanbi/ruanbirdAdd/" + this.$route.params.id,
          });
          break;
        case 2:
          // 观察
          this.$router.push({
            path: "/ruanbi/ruanbigcAdd/" + this.$route.params.id,
          });
          break;
        case 3:
          // 笔法
          this.$router.push({
            path: "/ruanbi/ruanbibfAdd/" + this.$route.params.id,
          });
          break;
        case 4:
          // 分析
          this.$router.push({
            path: "/ruanbi/ruanbifxAdd/" + this.$route.params.id,
          });
          break;
        case 5:
          // 对比
          this.$router.push({
            path: "/ruanbi/ruanbidbAdd/" + this.$route.params.id,
          });
          break;
        case 6:
          // 随堂练习
          this.$router.push({
            path: "/ruanbi/ruanbistlxAdd/" + this.$route.params.id,
          });
          break;
        default:
          // 视频
          this.$router.push({
            path: "/ruanbi/wordVideo/" + this.$route.params.id,
          });
          break;
      }
    },
    beforeVideoUpload(file) {
      const fileName = file.name;
      const fileType = fileName.substring(fileName.lastIndexOf("."));
      if (fileType !== ".mp4" && fileType !== ".flv") {
        this.$message.error("上传视频只能是mp4/flv!");
        return false;
      }
      return true;
    },
    //上传成功后的回调函数
    handleAudioSuccess(response) {
      this.contrastTypeface.contrastVideo = response.url;
    },
    // 超出上传限制
    handleExceed(files, fileList) {
      this.$message.warning(`只能上传一个视频`);
    },
    // 删除音频
    handleRemove(file, fileList) {
      this.contrastTypeface.contrastVideo = "";
    },
    //上传成功后的回调函数
    handleAvatarSuccess(response) {
      this.contrastTypeface.contrastSonpic = response.url;
    },
    getContrastByWordId() {
      this.dbLoading = true;
      this.$http({
        url: this.$http.adornUrl(
          `/sf/cbcontrast/getContrastByWordId/${this.contrast.tid}`
        ),
        method: "get",
      }).then(({ data }) => {
        this.dbList = [];
        this.xzBtn = false;
        if (data && data.code === 0) {
          if (data.cbContrast) {
            this.contrast = data.cbContrast;

            this.dbList.push(this.contrast);
            this.xzBtn = true;
          }
        } else {
          // this.contrast = { TypefaceName: "", editionCover: "", id: null };
        }
        this.dbLoading = false;
      });
    },
    // 章节对话框中的保存或修改
    saveOrUpdate() {
      if (this.type == 0) {
        // 添加章节
        this.contrastFormSubmit();
      } else {
        // 修改章节
        this.updateContrast();
      }
    },
    // 更新章节图片信息
    updateContrast() {
      this.$refs["contrastForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbcontrast/update`),
            method: "post",
            data: this.$http.adornData(
              {
                id: this.contrast.id,
                tid: this.contrast.tid,
                contrastDescription: this.contrast.contrastDescription,
              },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "修改成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getContrastByWordId();
                  this.dialogContrastVisible = false;
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 更新章节图片信息
    updateContrastTypeface() {
      this.$refs["contrastTypefaceForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbcontrasttypeface/update`),
            method: "post",
            data: this.$http.adornData(
              {
                id: this.contrastTypeface.id,
                cid: this.contrastTypeface.cid,
                contrastVideo: this.contrastTypeface.contrastVideo,
                corder: this.contrastTypeface.corder,
                contrastType: this.contrastTypeface.contrastType,
                contrastSonpic: this.contrastTypeface.contrastSonpic,
              },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "修改成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getContrastByWordId();
                  this.$refs["videoUpload"].clearFiles();
                  this.dialogContrastTypefaceVisible = false;
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 章节图片对话框中的保存或修改
    saveOrUpdate1() {
      if (this.type == 0) {
        // 添加章节
        this.contrastTypefaceFormSubmit();
      } else {
        // 修改章节
        this.updateContrastTypeface();
      }
    },
    // 打开对话框，添加、修改
    addContrast(status, id) {
      // 重置表单 应该只是把表单有的清空而已
      this.$nextTick(() => {
        this.$refs.contrastForm.resetFields();
      });
      if (status == 0) {
        // 添加
        this.type = 0;
        // 清空
        this.contrast.id = null;
      } else {
        // 修改
        this.type = 1;
        // 回显数据
        this.$http({
          url: this.$http.adornUrl("/sf/cbcontrast/info/" + id),
          method: "get",
        }).then(({ data }) => {
          this.contrast.id = data.cbContrast.id;
          this.contrast.contrastDescription =
            data.cbContrast.contrastDescription;
        });
      }
      this.dialogContrastVisible = true;
    },
    // 打开对话框，添加、修改
    addContrastTypeface(status, id) {
      // 重置表单 应该只是把表单有的清空而已
      this.$nextTick(() => {
        this.$refs.contrastTypefaceForm.resetFields();
      });
      if (status == 0) {
        // 添加
        this.type = 0;
        // 清空
        this.contrastTypeface.id = null;
        this.contrastTypeface.cid = id;
        this.contrastTypeface.contrastVideo = "";
        this.contrastTypeface.contrastSonpic = "";
      } else {
        // 修改
        this.type = 1;
        // 回显数据
        this.$http({
          url: this.$http.adornUrl("/sf/cbcontrasttypeface/info/" + id),
          method: "get",
        }).then(({ data }) => {
          this.contrastTypeface.id = data.cbContrastTypeface.id;
          this.contrastTypeface.contrastVideo =
            data.cbContrastTypeface.contrastVideo;
          this.contrastTypeface.cid = data.cbContrastTypeface.cid;
          this.contrastTypeface.corder = data.cbContrastTypeface.corder;
          this.contrastTypeface.contrastSonpic =
            data.cbContrastTypeface.contrastSonpic;
          this.contrastTypeface.contrastType =
            data.cbContrastTypeface.contrastType;
        });
      }
      this.dialogContrastTypefaceVisible = true;
    },
    // 表单提交
    contrastFormSubmit() {
      // 表单验证
      this.$refs["contrastForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbcontrast/save`),
            method: "post",
            data: this.$http.adornData(
              {
                contrastDescription: this.contrast.contrastDescription,
                tid: this.contrast.tid,
              },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "添加成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  // 刷新
                  this.getContrastByWordId();
                  // 只能添加一次
                  this.xzBtn = true;
                  this.dialogContrastVisible = false;
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 表单提交
    contrastTypefaceFormSubmit() {
      // 表单验证
      this.$refs["contrastTypefaceForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbcontrasttypeface/save`),
            method: "post",
            data: this.$http.adornData(
              {
                cid: this.contrastTypeface.cid,
                contrastVideo: this.contrastTypeface.contrastVideo,
                corder: this.contrastTypeface.corder,
                contrastType: this.contrastTypeface.contrastType,
                contrastSonpic: this.contrastTypeface.contrastSonpic,
              },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "添加成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  // 刷新
                  this.getContrastByWordId();
                  this.$refs["videoUpload"].clearFiles();
                  this.dialogContrastTypefaceVisible = false;
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 通过章节id删除章节
    deleteById(id) {
      this.$confirm(`确定对[欣赏id=${id}]进行[删除]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/sf/cbrd/delete"),
            method: "post",
            data: this.$http.adornData([id], false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "删除成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getContrastByWordId();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
    // 通过章节id删除章节
    deleteById2(id) {
      this.$confirm(`确定对[对比图片id=${id}]进行[删除]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/sf/cbcontrasttypeface/delete"),
            method: "post",
            data: this.$http.adornData([id], false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "删除成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getContrastByWordId();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
    // 上一步
    previous() {
      this.$router.push({
        path: "/ruanbi/word7",
      });
    },
    next() {
      // // 记录到哪一步了和id
      // this.$cookie.set("path", "ruanbistlxAdd");
      // this.$cookie.set("id", this.contrast.tid);
      // this.$router.push({
      //   path: "/ruanbi/ruanbistlxAdd/" + this.contrast.tid,
      // });
      Vue.cookie.delete("path");
      Vue.cookie.delete("id");
      this.$router.push({
        path: "/ruanbi/ruanbiInfo",
      });
    },
  },
};
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.chanpterList {
  position: relative;
  list-style: none;
  margin: 0;
  padding: 0;
}
.chanpterList li {
  position: relative;
}
.chanpterList p {
  float: left;
  font-size: 20px;
  margin: 10px 0;
  padding: 10px;
  height: 230px;
  line-height: 50px;
  width: 100%;
  border: 1px solid #ddd;
}
.chanpterList .acts {
  float: right;
  font-size: 14px;
}

.videoList {
  padding-left: 50px;
}
.videoList p {
  float: left;
  font-size: 14px;
  margin: 10px 0;
  padding: 10px;
  height: 50px;
  line-height: 30px;
  width: 100%;
  border: 1px dotted #ddd;
}
</style>
